<template>
	<view class="content">
		<u-modal :show="show" :title="title" @confirm="show=false">
			<view class="slot-content">
				客服电话
			</view>
		</u-modal>
		<view class="flexImg" @click="login">
			<view class="herdericon">
				<img class="iconimg" :src="usrimg" alt="">
			</view>
			<view class="iconright">{{nickName?nickName:'立即登录'}}</view>
		</view>
		<u-cell-group>
			<u-cell icon="setting-fill" title="我的宠物" arrow-direction="down" @click="tomypet">
			</u-cell>
			<u-cell icon="setting-fill" title="在线客服" @click="show=true"></u-cell>
			<u-cell icon="setting-fill" title="系统设置"></u-cell>
			<u-cell icon="setting-fill" title="时光机介绍" @click="toaboutme"></u-cell>
		</u-cell-group>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {getlogin,poslogin} from "@/pages/api/apihost.js"
	import mixins from "@/pages/mxin/mxin.js"
	export default {
		mixins:[mixins],
		data() {
			return {
				title: '在线客服',
				show:false,
				istoken:'',
				userinfo:{},
				usrimg:'',
				nickName:'',
			}
		},
		onLoad() {},
		methods: {
		 async login(){
			 let t=this
			 await uni.getUserProfile ({
					desc:'用于用户登录',
					success(usr) {
						t.userinfo=usr;
						console.log(usr, 'user')
					},
					fail(e) {
						t.showtost('授权失败,请稍后再试')
					}
				})
				await wx.login({
					success:(r)=>{
						getlogin({code:r.code}).then(r=>{
							if(r.isRegisterUser){
								// 存token
								t.setuser()
								uni.setStorageSync('token', r.loginToken);
							} else {
								t.faileLogin(r)
							}
							
						})
					}
				})
			},
			setuser(){
				let id=setTimeout(()=>{
					console.log(this.userinfo, '345')
					this.usrimg=this.userinfo.userInfo.avatarUrl
					this.nickName=this.userinfo.userInfo.nickName
					clearTimeout(id)
				},1000)
			},
			faileLogin(r){
				let id=setTimeout(()=>{
					console.log(this.userinfo, 'data1234')
					poslogin({
						openId:r.openid,
						nickName:this.userinfo.userInfo.nickName,
						avatarUrl:this.userinfo.userInfo.avatarUrl
					}).then(data=>{
						uni.setStorageSync('token', data);
					})
					clearTimeout(id)
				},500)
			},
			tomypet() {
				uni.navigateTo({
					url: "/petselfinfo/mypet"
				})
			},
			toaboutme() {
				uni.navigateTo({
					url: "/petselfinfo/aboutme"
				})
			}
		},
	}
</script>

<style lang="scss">
	.content{
		padding: 24rpx;
	}
	.flexImg {
		display: flex;
		padding-top: 30rpx;
		padding-bottom: 60rpx;
	}

	.herdericon {
		.iconimg {
			width: 120rpx;
			height: 120rpx;
			border-radius: 60rpx;
			overflow: hidden;
			background: red;
		}
	}

	.iconright {
		line-height: 120rpx;
		height: 120rpx;
		padding-left: 24rpx;
	}
</style>